

#include "includes.h"
#include <string.h> 
#include <stdio.h>  
#include <stdbool.h>
#include <IOdevice.h>
#include "xrj_protocol.h"
#include "ultrasonic.h"
#include "lm75a.h"

extern UINT32  LOCK_OPEN_Time;






#define	GPIO_Pin_BEEP	GPIO_Pin_13


#define	P_Out_BEEP	 	PCout(13)

UINT32 Mode_Beep = 0;
UINT32 Mode_Beep_Backup = 0;
UINT32 Mode_Beep_Cnt = 0;
void BEEP_Init(void)
{			

	
	GPIO_InitTypeDef  GPIO_InitStructure; 
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC |RCC_APB2Periph_AFIO, ENABLE); 
	
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_BEEP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
	GPIO_Init(GPIOC, &GPIO_InitStructure);

  P_Out_BEEP = 0;
}

void BEEP_Mode_Set(UINT32 mode)
{
	Mode_Beep = mode;
}	


void BEEP_On(void)
{
  P_Out_BEEP = 1;
}	

void BEEP_Off(void)
{
	P_Out_BEEP = 0;
}	

void BEEP_LOOP(void)
{
	if (Mode_Beep != 0)
	{

		Mode_Beep_Cnt = 0;
		Mode_Beep_Backup = Mode_Beep;
		Mode_Beep = 0;
	}
	
	if (Mode_Beep_Backup != 0)
	{
		
		if (Mode_Beep_Cnt == 0)
		{
			BEEP_On();
		}	
		else if (Mode_Beep_Cnt > 30)	
		{
			BEEP_Off();
			Mode_Beep_Backup = 0;
		}	
		Mode_Beep_Cnt++;
		
	}
}


uint8_t Beep_Timer_Cnt = 0;
#define	Timer_Cnt_Num	2	
void App_TimeTickHook (void)
{

	Beep_Timer_Cnt++;
	if (Beep_Timer_Cnt >= Timer_Cnt_Num)
	{
		Beep_Timer_Cnt = 0;	
		BEEP_LOOP();
	}
}	




void IOdevice_task(void *pdata)
{
	while(1)
	{	
		OSTimeDlyHMSM (0,0,0,10);
		KEY_Proc();
		IN_Proc();
		LED_Proc();
		Motor_Proc();
		FAN_PROC();
		LOCK_PROC();
		Weight_Proc();
		LM75A_PROC();
	/*	OSTimeDlyHMSM (0,0,2,0);
		P_Out_LED1 = 1;
		P_Out_LED2 = 1;
		P_Out_Motor1_P = 1;
		P_Out_Motor2_P = 1;
		P_Out_Motor1_N = 0;
		P_Out_Motor2_N = 0;		
		P_Out_LIGHT = 1;
		OSTimeDlyHMSM (0,0,2,0);
		P_Out_LED1 = 0;
		P_Out_LED2 = 0;
		P_Out_Motor1_P = 0;
		P_Out_Motor2_P = 0;
		P_Out_Motor1_N = 1;
		P_Out_Motor2_N = 1;
		P_Out_LIGHT = 0;	*/	
	}	
}
